package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.OrderSettingListService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSettingList")
public class OrderSettingListController {
    @Reference
    OrderSettingListService orderSettingListService;

    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean bean) {

        // PageResult result = service.findPage(bean);

        return orderSettingListService.findPage(bean);

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Object[] obj){
        //将json数据转换成集合
       try {
           List<Integer> ids = JSON.parseArray(obj[0].toString(), Integer.class);
           Map map = JSON.parseObject(obj[1].toString(), Map.class);
           if ( ids.size()<=0){
               return new Result(false,"需要选择体检套餐" );
           }
           for (Integer id : ids) {
               orderSettingListService.add(map,id);
           }
           return new Result(true, MessageConstant.ADD_ORDERLISTSETTING_SUCCESS );
       }catch (Exception e){
           e.printStackTrace();
           return new Result(false,MessageConstant.ADD_ORDERLISTSETTING_FAIL );
       }

    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderSettingListService.delete(id);
            return new Result(true,"预约取消成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"预约取消失败" );
        }
    }

    @RequestMapping("/updata")
    public Result updata(Integer id){
        try {
            return orderSettingListService.updata(id);
        }catch (Exception e){
            e.printStackTrace();
            return orderSettingListService.updata(id);
        }
    }

}
